package com.hong.utils.executors;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Auther: Administrator
 * @Date: 2018/7/31 10:23
 * @Description:
 */
public class CompletableFutureExample3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");

        future.thenAcceptAsync(System.out::println);
        future.thenRun(()-> System.out.println("done")).thenRunAsync(()-> System.out.println("done"));

        /*CompletableFuture<Integer> future = CompletableFuture.supplyAsync((Supplier<String>)()->{
            throw new RuntimeException("not get the data");
        }).handleAsync((s,t)->{
            Optional.of(t).ifPresent(e-> System.out.println("Error"));
            return (s==null)?0:s.length();
        });
        System.out.println(future.get());*/
                /*.thenApplyAsync(s->{
                    try {
                        System.out.println("===========");
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("=====over=====");
                    return  s.length();
                });*/
//                .thenApply(String::length);
               /* future.whenComplete((v,t)->{
                    try {
                        System.out.println("===========");
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("======over==========");
                });
        System.out.println("4444444444444444");*/

       /* future.whenCompleteAsync((v,t)->{
                        try {
                            System.out.println("===========");
                            TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("======over==========");
                    });*/

        System.out.println(future.get());
        Thread.currentThread().join();
    }
}
